#include "atlas_kernel.cuh"
#include <string.h>
#include "cuda_helper.h"

#include "atlas.hh"

SpaceObjectAtlas::SpaceObjectAtlas(){
	m_host = NULL;
	m_count = 0;
	m_device = NULL;
}

SpaceObjectAtlas::~SpaceObjectAtlas(){
	if(m_count > 0){
		if(m_host != NULL) free(m_host);
		if(m_device != NULL) CUDA_CALL(cudaFree(m_device));
	}
}

void SpaceObjectAtlas::calcSqrs(){
	if(m_device == NULL){
		CUDA_CALL(cudaMalloc((void**)&m_device, sizeof(SOA_Data) * m_count));
	}
	CUDA_CALL(cudaMemcpy(m_device, m_host, sizeof(SOA_Data) * m_count, cudaMemcpyHostToDevice));
	atlas_cuda_calcSqrs(m_device, m_count);
	CUDA_CALL(cudaMemcpy(m_host, m_device, sizeof(SOA_Data) * m_count, cudaMemcpyDeviceToHost));
}

void SpaceObjectAtlas::setAtlas(SOA_Data *data, unsigned int count){
	m_host = (SOA_Data *)malloc(sizeof(SOA_Data) * count);
	m_count = count;
	memcpy(m_host, data, sizeof(SOA_Data) * count);
}

SpaceObjectAtlas::SOA_Data *SpaceObjectAtlas::getHost(){
	return m_host;
}

SpaceObjectAtlas::SOA_Data *SpaceObjectAtlas::getDevice(){
	return m_device;
}

unsigned int SpaceObjectAtlas::getCount(){
	return m_count;
}
